package com.wusuowei.shiro_jwt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wusuowei.shiro_jwt.model.po.Product;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface ProductMapper extends BaseMapper<Product> {
    @Select("SELECT * FROM product WHERE id = #{id} AND is_delete = 1")
    Product getDeletedProductById(Integer id);



        @Select("SELECT product.id, product.pro_name AS proName, product.pro_price, product.p_avatar_url, product.cid, " +
                "product.create_time, product.introduction, product.is_delete, category.id AS cId, category.c_name, category.is_delete " +
                "FROM product " +
                "JOIN category ON product.cid = category.id " +
                "WHERE product.is_delete != 1 " +
                "AND (#{proname} IS NULL OR product.pro_name LIKE CONCAT('%', #{proname}, '%')) " +
                "AND (#{cname} IS NULL OR category.c_name LIKE CONCAT('%', #{cname}, '%')) " +
                "AND (#{proprice} IS NULL OR CAST(product.pro_price AS CHAR) LIKE CONCAT('%', #{proprice}, '%'))")

        Page<Product> findPage(Page<Product> page, @Param("proname") String proname, @Param("cname") String cname, @Param("proprice") String proprice);

    /**
     * 查询记录数
     * @param proname
     * @param cname
     * @param proprice
     * @return
     */
        @Select("SELECT COUNT(*) " +
            "FROM product p " +
            "JOIN category c ON p.cid = c.id " +
            "WHERE p.is_delete = 0 " +
            "AND (#{proname} IS NULL OR p.pro_name LIKE CONCAT('%', #{proname}, '%')) " +
            "AND (#{cname} IS NULL OR c.c_name LIKE CONCAT('%', #{cname}, '%')) " +
            "AND (#{proprice} IS NULL OR CAST(p.pro_price AS CHAR) LIKE CONCAT('%', #{proprice}, '%'))")
    int countProducts(@Param("proname") String proname, @Param("cname") String cname, @Param("proprice") String proprice);
}


